﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace QH_MSS_WPF_Core.UserControls
{
    /// <summary>
    /// NumericKeyBoardUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class NumericKeyBoardUserControl : Window
    {
        private int TextIndex { get; set; }
        public string TextStr { get; private set; }//通过该属性，访问Keyboard的文本

        public NumericKeyBoardUserControl(string inputStr)//构造方式传入初始文本
        {
            InitializeComponent();

            TextStr = inputStr;
            tbValue.Text = inputStr;
            tbValue.Focus();
            tbValue.CaretIndex = inputStr.Length;
           
        }
        private void tbValue_TextChanged(object sender, RoutedEventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            TextIndex = textBox.CaretIndex; 
        }

        private void Button_TouchUp(object sender, TouchEventArgs e)
        {
            Button button = (Button)sender;
            if (TextIndex == 0)
            {
                tbValue.Text += (string)button.Content;
            }
            else
            {
                tbValue.Text = tbValue.Text.Insert(TextIndex, (string)button.Content);
            }
        }
        /// <summary>
        /// del
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_TouchUp_1(object sender, TouchEventArgs e)
        {
            if (tbValue.Text.Length > 0)
            {
                if (TextIndex == 0 && tbValue.Text.Length >= 1)
                {
                    tbValue.Text = tbValue.Text.Remove(tbValue.Text.Length - 1, 1);
                }
                else if (TextIndex > 0)
                {
                    tbValue.Text = tbValue.Text.Remove(TextIndex - 1, 1);
                }
            }
        }
        //Clear
        private void Button_TouchUp_2(object sender, TouchEventArgs e)
        {
            tbValue.Text = "";
        }
        //Cancel
        private void Button_TouchUp_3(object sender, TouchEventArgs e)
        {
            
            DialogResult = false;
            Close();
        }
        //OK
        private void Button_TouchUp_4(object sender, TouchEventArgs e)
        {
            TextStr = tbValue.Text;
            DialogResult = true;
            Close();
        }

      
    }
}
